from django.db import models
from PMSP.utils.base_model import BaseModel
from django.contrib.auth.models import User

# 1,订单模型类
class OrderInfo(BaseModel):

    # 支付方式
    PAY_METHOD_CHOICES = (
        (1,"货到付款"),
        (2,"支付宝")
    )

    # 订单状态
    ORDER_STATUS_CHOICES = (
        (1,"待支付"),
        (2,"待发货"),
        (3,"待收货"),
        (4,"待评价"),
        (5,"已完成"),
        (6,"已取消")
    )

    # 主键如果自己指定了，那么系统不会帮我们生成id主键了
    order_id = models.CharField(max_length=64,primary_key=True,verbose_name="订单号")
    user = models.ForeignKey(User,on_delete=models.PROTECT,verbose_name="下单用户")
    address = models.ForeignKey('users.Address',on_delete=models.PROTECT,verbose_name="下单地址")
    total_count = models.IntegerField(default=1,verbose_name="商品总数")
    total_amount = models.DecimalField(max_digits=10,decimal_places=2,verbose_name="商品总金额")
    feight = models.DecimalField(max_digits=5,decimal_places=2,verbose_name="运费")
    pay_method = models.SmallIntegerField(choices=PAY_METHOD_CHOICES,default=2,verbose_name="支付方式")
    status = models.SmallIntegerField(choices=ORDER_STATUS_CHOICES,default=1,verbose_name="订单状态")

    class Meta:
        db_table = "tb_order_info"
        verbose_name = "订单基本信息"

# 2，订单商品模型类
class OrderGoods(BaseModel):

    # 商品评分
    SCORE_CHOICES = (
        (0,"0分"),
        (1,"20分"),
        (2,"40分"),
        (3,"60分"),
        (4,"80分"),
        (5,"100分")
    )

    # related_name关联属性，通过order.skus可以知道当前这个订单中关联了哪些订单商品
    order = models.ForeignKey(OrderInfo,related_name="skus",on_delete=models.CASCADE,verbose_name="订单")
    sku = models.ForeignKey('goods.SKU',on_delete=models.PROTECT,verbose_name="订单商品")
    count = models.IntegerField(default=1,verbose_name="数量")
    price = models.DecimalField(max_digits=10,decimal_places=2,verbose_name="单价")
    comment = models.TextField(default="",verbose_name='评价信息')
    score = models.SmallIntegerField(choices=SCORE_CHOICES,default=5,verbose_name="商品满意度评分")
    is_anonymous = models.BooleanField(default=False,verbose_name="是否匿名")
    is_commented = models.BooleanField(default=False,verbose_name="是否评价")

    class Meta:
        db_table = "tb_order_goods"
        verbose_name = "订单商品"
